<template>
  <a-spin :spinning="confirmLoading">
    <a-form :form="form">
      <a-row>
        <a-col :span="8">
          <a-form-item label="团长名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input
              :disabled="formDisabled"
              v-decorator="['shopName', validatorRules.shopName]"
              placeholder=""
            ></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="团长电话" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input :disabled="title !== '查看'" v-decorator="['tel', validatorRules.tel]" placeholder=""></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="经" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input :disabled="title !== '查看'" v-decorator="['longitude']" placeholder=""></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="纬" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input :disabled="title !== '查看'" v-decorator="['latitude']" placeholder=""></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="地址" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input :disabled="title !== '查看'" v-decorator="['location']" placeholder=""></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="8" v-if="title == '查看'">
          <a-form-item label="详细地址" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input :disabled="title !== '查看'" v-decorator="['address']" placeholder=""></a-input>
          </a-form-item>
        </a-col>

        <a-col :span="8">
          <a-form-item label="开户姓名" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input :disabled="formDisabled" v-decorator="['accountName']" placeholder=""></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="银行卡号" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input :disabled="formDisabled" v-decorator="['bankNo']" placeholder=""></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="开户行" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input :disabled="formDisabled" v-decorator="['bankName']" placeholder=""></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <j-dict-select-tag
              type="list"
              v-decorator="['status']"
              :trigger-change="true"
              :disabled="formDisabled"
              dictCode="approve_status"
              placeholder=""
            />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span="8" v-show="['上线', '查看', '下线'].indexOf(title) > -1">
          <a-form-item label="原因" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-textarea  v-decorator="['remark', validatorRules.remark]" placeholder="" />
<!--            <a-textarea :disabled="title == '查看'" v-decorator="['remark', validatorRules.remark]" placeholder="" />-->
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <span class="remark"
            >注：团长审批，驳回操作时，此处应填写驳回的原因，该原因显示在小程序的前端展示给用户。</span
          >
        </a-col>
        <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
          <a-button @click="submitForm">提 交</a-button>
        </a-col>
      </a-row>
    </a-form>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import pick from 'lodash.pick'
import { postShopInfoApprove, postShopInfoOffline } from '@/api/teamleader'
import { validateDuplicateValue } from '@/utils/util'
import JFormContainer from '@/components/jeecg/JFormContainer'
import JDictSelectTag from '@/components/dict/JDictSelectTag'

export default {
  name: 'ShopInfoForm',
  components: {
    JFormContainer,
    JDictSelectTag,
  },
  props: {
    //流程表单data
    formData: {
      type: Object,
      default: () => {},
      required: false,
    },
    //表单模式：true流程表单 false普通表单
    formBpm: {
      type: Boolean,
      default: false,
      required: false,
    },
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
    title: {
      type: String,
      default: '',
      required: false,
    },
  },
  data() {
    return {
      form: this.$form.createForm(this),
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        shopName: {
          rules: [{ required: true, message: '请输入团长名称!' }],
        },
        tel: {
          rules: [{ required: false }, { pattern: /^1[3456789]\d{9}$/, message: '请输入正确的手机号码!' }],
        },
        remark: {
          rules: [{ required: true, message: '请输入备注!' }],
        },
      },
      url: {
        add: '/shopInfo/shopInfo/add',
        edit: '/shopInfo/shopInfo/edit',
        queryById: '/shopInfo/shopInfo/queryById',
      },
    }
  },
  computed: {
    formDisabled() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return false
        }
        return true
      }
      return this.disabled
    },
    showFlowSubmitButton() {
      if (this.formBpm === true) {
        if (this.formData.disabled === false) {
          return true
        }
      }
      return false
    },
  },
  created() {
    //如果是流程中表单，则需要加载流程表单data
    this.showFlowData()
  },
  methods: {
    handleApp(status, id) {
      this.form.validateFields(['remark'], (errors, values) => {
        if (values.remark) {
          if (status !== 3) {
            this.handleup(status, id, values.remark)
          } else {
            this.handledo(status, id, values.remark)
          }
        }
      })
    },
    handleup(status, id, remark) {
      this.confirmLoading = true
      postShopInfoApprove({ status, id, remark })
        .then((res) => {
          if (res.success) {
            this.$message.success(res.message)
            this.$emit('ok')
          } else {
            this.$message.warning(res.message)
          }
        })
        .finally(() => {
          this.confirmLoading = false
        })
    },
    handledo(status, id, remark) {
      postShopInfoOffline({ status, id, remark })
        .then((res) => {
          if (res.success) {
            this.$message.success(res.message)
            this.$emit('ok')
          } else {
            this.$message.warning(res.message)
          }
        })
        .finally(() => {
          this.confirmLoading = false
        })
    },
    add() {
      this.edit({})
    },
    edit(record) {
      this.form.resetFields()
      this.model = Object.assign({ remark: '' }, record)
      this.$nextTick(() => {
        this.form.setFieldsValue(
          pick(
            this.model,
            'shopName',
            'tel',
            'longitude',
            'latitude',
            'location',
            'status',
            'bankNo',
            'bankName',
            'accountName',
            'address',
            'remark'
          )
        )
      })
    },
    //渲染流程表单数据
    showFlowData() {
      if (this.formBpm === true) {
        let params = { id: this.formData.dataId }
        getAction(this.url.queryById, params).then((res) => {
          if (res.success) {
            this.edit(res.result)
          }
        })
      }
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          let formData = Object.assign(this.model, values)
          // console.log('表单提交数据', formData)
          httpAction(httpurl, formData, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
    popupCallback(row) {
      this.form.setFieldsValue(
        pick(
          row,
          'shopName',
          'tel',
          'longitude',
          'latitude',
          'location',
          'status',
          'bankNo',
          'bankName',
          'accountName',
          'address'
        )
      )
    },
  },
}
</script>

<style>
.remark {
  color: #ff7f36;
  line-height: 120% !important;
}
</style>
